package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerHerizontalLinearItemDecoration;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.module.excitation.adapter.SmallachievementAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.AchievementObject;
import com.lishe.lishe.lishe.module.excitation.bean.AchievementVoListObject;
import com.lishe.lishe.lishe.utils.Utils;

/**
 * Created by DELL on 2017/10/30.
 */

public class AchievementLightUpDialog extends Dialog {


    public AchievementLightUpDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements View.OnClickListener {
        private Context context;
        private AchievementObject achievement;
        private AchievementLightUpDialog dialog;
        private ImageLoaderUtils utils;

        public Builder(Context context) {
            this.context = context;
            utils = new ImageLoaderUtils();
        }

        public Builder setAchievementObject(AchievementObject achievement) {
            this.achievement = achievement;
            return this;
        }


        public AchievementLightUpDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new AchievementLightUpDialog(context, R.style.MyDialog);
                View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_ahievement_light_up, null);
                ImageView img = contentView.findViewById(R.id.img);
                ImageView dissmiss = contentView.findViewById(R.id.dissmiss);

                RecyclerView listView=contentView.findViewById(R.id.listView);
                listView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                listView.addItemDecoration(new RecyclerHerizontalLinearItemDecoration(0, Utils.dip2px(context,25)));

                TextView text = contentView.findViewById(R.id.text);


                if (achievement == null) {
                    throw new NullPointerException("do you setAchievementObject?");
                } else {
                    SmallachievementAdapter adaper=new SmallachievementAdapter(achievement.getAchievementVoLists());
                    listView.setAdapter(adaper);
                    utils.displayImage(achievement.getAchieveImg(), img);
                    if (achievement.getAchievementVoLists() != null) {
                        int j = 0;
                        for (int i = 0; i < achievement.getAchievementVoLists().size(); i++) {
                            AchievementVoListObject object = achievement.getAchievementVoLists().get(i);
                            if (!object.getUserId().equals("0")) {
                                j++;
                            }
                        }
                        text.setText("点亮以下" + j + "个成就");
                    }
                }

                dissmiss.setOnClickListener(this);
                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        dialog.cancel();
                    }
                });

                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }
            return dialog;
        }

        public void show() {
            try {
                create();
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            dialog.dismiss();
            dialog.cancel();
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dissmiss:
                case R.id.sure:
                    dissmiss();
                    break;
            }
        }
    }

}
